﻿Imports BR.Conexiones.Mantenimiento.Administracion.Seguridad
Imports StartFrame.Web.BR.Helpers
Imports StartFrame.Web.Handlers

Namespace Handlers.Consultas

    Public Class Recorridos
        Inherits baseHandler

#Region "Constructor"
        ''' <summary>
        ''' Manejador (handler) de los eventos (acciones) asociados a la configuración de los usuarios registrados en el sistema
        ''' </summary>
        Public Overrides Sub InicializarDatos()
            MyBase.InicializarDatos()
            _Abm = New BR.Conexiones.Consultas.Recorridos()
            _Pagina = "~/UserPages/Consultas/Recorridos.js"
        End Sub
#End Region

#Region "Metodos"
        Public Sub ObtenerTipoRecorrida()
            _Context.Response.ContentType = "text/plain"
            _Context.Response.Write(CType(_Abm, BR.Conexiones.Consultas.Recorridos).ObtenerTipoRecorrida(Common.Env.Operador))
        End Sub

        Public Sub CrearPdf()
            Dim abm As BR.Conexiones.Consultas.Recorridos = _Abm
            If _RequestData("tipoExportacion") = "PDF" Then
                Try
                    abm.CrearPdf(_Context.Response.OutputStream, abm.ObtenerListadoRecorridas(_RequestData("fechas"), _RequestData("idRecorrida")))
                    _Context.Response.ContentType = "application/pdf"
                    _Context.Response.AddHeader("Content-Disposition", String.Format("attachment;filename=Receipt-{0}.pdf", "Recorrida"))
                    _Context.Response.Cookies.Add(New HttpCookie("ceUniqueValue", _RequestData("cookieValue")))
                Catch ex As Exception
                    _Context.Response.Clear()
                    _Context.Response.ClearContent()
                    _Context.Response.ContentType = "text/plain"
                    _Context.Response.Cookies.Add(New HttpCookie(_RequestData("cookieValue") + "_exMessage", ex.Message))
                    _Context.Response.Write(ex.Message)
                End Try
            End If
            If _RequestData("tipoExportacion") = "EMAIL" Then
                _Context.Response.ContentType = "text/plain"
                _Context.Response.Cookies.Add(New HttpCookie("ceUniqueValue", _RequestData("cookieValue")))
                _Context.Response.Write("OK")
                Dim mStream As New System.IO.MemoryStream
                abm.CrearPdf(mStream, abm.ObtenerListadoRecorridas(_RequestData("fechas"), _RequestData("idRecorrida")), False)
                abm.EnviarEmail(mStream, "Recorrida.pdf")
            End If
        End Sub

        Public Sub ObtenerDias()
            _Context.Response.ContentType = "text/plain"
            Dim result As New ResultSet
            Try
                result.LoadTable(CType(_Abm, BR.Conexiones.Consultas.Recorridos).ObtenerDias(Common.Env.Operador))
            Catch ex As Exception
                result.success = False
                result.message = ex.Message
            End Try
            _Context.Response.Write(result.ToJsonString())
        End Sub

        Public Sub ObtenerRecorridas()
            _Context.Response.ContentType = "text/plain"
            Dim result As New ResultSet
            Try
                result.LoadTable(CType(_Abm, BR.Conexiones.Consultas.Recorridos).ObtenerRecorridas(Common.Env.Operador))
            Catch ex As Exception
                result.success = False
                result.message = ex.Message
            End Try
            _Context.Response.Write(result.ToJsonString())
        End Sub

#End Region

    End Class
End Namespace